#include "Signal.h"
#include "Device.h"
#include "SleepTimer.h"
#include <stdio.h>

Signal::Signal(string name, Device* pDevice, int freq)
	: _name(name), _pInput(pDevice), _time(0)
{
	setFrequency(freq);
};

string Signal::getName() 
{
	return _name;
}

int Signal::getFrequency()
{
	return _frequency;
}

int Signal::getSleepTime()
{
	return _sleeptime;
}

void Signal::setFrequency(int freq)
{
	_frequency = freq;
	_sleeptime = 1000/freq;
}

void Signal::run()
{
	SleepTimer t1;

	while(1)
	{
		update();
		
		_history.store(_reading); 
		t1.sleep(_sleeptime);
	}
}

void Signal::update()
{
	_reading.value = _pInput->read();
	_reading.timestamp = _time++;
} 

History* Signal::getHistory()
{
	return &_history;
}
